﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon.Prompt;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class ArrivalRegister : EFViewCmes
    {
        public string InsertMeg = "";
        public ArrivalRegister(string dialogType)
        {
            InitializeComponent();
            Init(dialogType);
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    this.Commit.Visibility = Visibility.Visible;
                    this.Next.Visibility = Visibility.Collapsed;
                    base.Mode = OperationMode.Insert;
                    break;
                case "stockinsert":
                    base.Mode = OperationMode.Insert;
                    this.fdSupplyName.Visibility = Visibility.Collapsed;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;

            if (this.detailsList.Visibility == Visibility.Collapsed)
            {
                base.OnCommitButtonClick(this.eServiceName, sender, e);
                return;
            }

            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (this.grid.GetCheckedItems().Count == 0)
            {
                this.ValidateMessagePopup("请勾选一条或多条记录！");//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            string uniqueName = "";
            foreach (EIRow row in this.grid.CheckedEIRows)
            {
                if (uniqueName.Equals(""))
                {
                    uniqueName = row["fdSupplyName"].ToString();
                }
                else
                {
                    if (!uniqueName.Equals(row["fdSupplyName"]))
                    {
                        this.ValidateMessagePopup("选择的记录中，有不同的供应商，请重新选择！");//弹出提示信息
                        this.TempButtonOpen(true);//确定按钮生效
                        return;
                    }
                }
            }

            this.fdSupplyName.Value = uniqueName;

            List<string> list = new List<string>();
            bool flag = this.grid.Validate(DataRange.CheckedItems, out list);

            if(list.Count == 0)
            {
                EIInfo info = this.BuildSubmitEIInfo(this.eServiceName, Mode.ToString().ToLower(), "result");
                EIBlock block = info.AddBlock("subBlock");
                block.SetColumns(this.grid.EIBlock.Columns);
                block.SetRows(this.grid.CheckedEIRows);
                base.Submit(info, DoCommitCompleted);
            }
            else
            {
                string meg = "有错误：";
                foreach (string str in list)
                {
                    meg += str + ";";
                }
                this.ValidateMessagePopup(meg);//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 下一步按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NextClick(object sender, RoutedEventArgs e)
        {
            if (this.ValidateField())
            {
                this.spWindows.Visibility = Visibility.Collapsed;
                this.detailsList.Visibility = Visibility.Visible;
                this.Commit.Visibility = Visibility.Visible;
                this.Next.Visibility = Visibility.Collapsed;
                EFWindow wid = this.Parent as EFWindow;
                wid.UpdateLayout();
            }
        }

        /// <summary>
        /// 提示错误消息
        /// </summary>
        /// <param name="str"></param>
        private void ValidateMessagePopup(string str)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(str);
            pop.Show(6);

            return;
        }

        /// <summary>
        /// 点击"确定"后的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                InsertMeg = inInfo.GetDetailMsg();//新增后的回调方法，用于提醒用户，如需调整计划价，请进入调价模块
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        private void QueryClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCMF2");
            eiInfo.Set("method", "query");
            EIBlock block = eiInfo.AddBlock(EIConstants.QueryStatusBlock);
            if(this.inqufdYear.Value != null && !this.inqufdYear.Value.Equals(""))
            {
                EIColumn column = new EIColumn();
                column.SetName("fdYear");
                block.AddColumn(column);

                EIRow row = new EIRow();
                row.Add("fdYear", this.inqufdYear.Value);
                block.AddRow(row);
            }
            if (this.inqufdMonth.Value != null && !this.inqufdMonth.Value.Equals(""))
            {
                EIColumn column = new EIColumn();
                column.SetName("fdMonth");
                block.AddColumn(column);

                EIRow row = new EIRow();
                row.Add("fdMonth", this.inqufdMonth.Value);
                block.AddRow(row);
            }
            if (this.inqufdMaterialName.Value != null && !this.inqufdMaterialName.Value.Equals(""))
            {
                EIColumn column = new EIColumn();
                column.SetName("fdMaterialName");
                block.AddColumn(column);

                EIRow row = new EIRow();
                row.Add("fdMaterialName", this.inqufdMaterialName.Value);
                block.AddRow(row);
            }
            if (this.inqufdSpecModel.Value != null && !this.inqufdSpecModel.Value.Equals(""))
            {
                EIColumn column = new EIColumn();
                column.SetName("fdSpecModel");
                block.AddColumn(column);

                EIRow row = new EIRow();
                row.Add("fdSpecModel", this.inqufdSpecModel.Value);
                block.AddRow(row);
            }
            if (this.inqufdSupplyName.Value != null && !this.inqufdSupplyName.Value.Equals(""))
            {
                EIColumn column = new EIColumn();
                column.SetName("fdSupplyName");
                block.AddColumn(column);

                EIRow row = new EIRow();
                row.Add("fdSupplyName", this.inqufdSupplyName.Value);
                block.AddRow(row);
            }

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, OnDoQueryCompleted);
        }

        /// <summary>
        /// 查询后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoQueryCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() != -1)
            {
                EIInfo info = new EIInfo();
                EIBlock block = info.AddBlock("subBlock");
                block.SetColumns(e.EIInfo.GetBlock("result").GetColumns());
                block.SetRows(e.EIInfo.GetBlock("result").GetRows());
                this.grid.UpdateData(info);
            }
        }
    }
}

